<?php
class indexController extends siteController
{
    public function index()
    {
    	$form=substr(in(urldecode($_GET['form'])),0,15);
    	if(empty($form)){
    		$this->error404();
    	}
    	//获取表单信息
    	$info=model('form')->table_info($form);
    	if(empty($info)||!$info['display']){
    		$this->error404();
    	}
        $this->field_list=model('form_field')->field_list($info['id']);

        //导航
        $this->nav=array(
            0=>array('name'=>$info['name'],'curl'=>url('index/index',array('form'=>$form))),
        );

        //MEDIA信息
        $this->common=api('default','common_media',array('title'=>$info['name'],'keywords'=>$info['name'],'description'=>$info['name']));

        //设置分页
        $size = $info['page']; 
        if (empty($size)) {
            $listrows = 10;
        } else {
            $listrows = $size;
        }
        $url=url('index/index',array('form'=>$form,'page'=>'{page}'));
        $limit=$this->pageLimit($url,$listrows);

        //内容列表
        $this->loop=model('form_field_data')->form_list($info['table'],$limit,$info['order'],$info['where']);
        //统计总内容数量
        $count=model('form_field_data')->form_count($info['table'],$info['where']);

        //获取分页
        $this->page=$this->pageShow($count);

        $this->info=$info;
        $this->count=$count;

        $this->show($info['tpl']);
    }

    //提交表单
    public function post()
    {
    	$form=in($_POST['form']);
    	if(empty($form)){
    		$this->error404();
    	}
    	$info=model('form')->table_info($form);
    	if(empty($info)||!$info['display']||!$info['release']){
    		$this->error404();
    	}
    	//获取所有字段
    	$field_list=model('form_field')->field_list($info['id']);
    	if(empty($field_list)){
            if(is_ajax()){
                $this->msg('未发现表单信息！',0);
            }else{
                $this->alert('未发现表单信息!');
            }
            return;
        }
        //过滤机器信息
        if(!empty($_POST['test_value'])){
        	return;
        }

        $data=array();
        foreach ($field_list as $value) {
            $verification=unserialize($value['verification']);
            if(!empty($verification)){
                if(empty($_POST[$value['field']])){
                    if(is_ajax()){
                        $this->msg($value['name'].'不能为空！',0);
                    }else{
                        $this->alert($value['name'].'不能为空！');
                    }
                }
            }
            $data[$value['field']]=api('expand_model','field_in',array('data'=>$_POST,'type'=>$value['type'],'field'=>$value['field']));
            if(!isset($_POST[$value['field']])){
                $data[$value['field']]=$value['default'];
            }
        }

        if($info['verify']){
            if ($_POST['checkcode'] != $_SESSION['verify']) {
                if(is_ajax()){
                    $this->msg('验证码错误!',0);
                }else{
                    $this->alert('验证码错误!');
                }
            }
        }

        //过滤完后提交表单
        $data['fid']=$info['id'];
        model('form_field_data')->add($data);
        if($info['return_url']){
            $url=$this->display($info['return_url'],true,false);
        }else{
            $url=url('index/index',array('form'=>$form));
        }
        if(is_ajax()){
            $this->msg($info['return_msg']);
        }else{
            $this->alert($info['return_msg'],$url);
        }
    }

    //验证码
    public function verify(){
        Image::buildImageVerify();
    }

}

?>